<?php

namespace app\admin\controller;

use app\common\model\Tag as TagModel;
use app\common\lib\Util;
use think\Request;

class Tag extends Base
{
    //好友关系名列表
    public function index()
    {
        $tagList = TagModel::paginate(15);
        $this->assign('title', '标签');
        $this->assign('list', $tagList);
        return $this->fetch();
    }

    //新增
    public function create(){
        return $this->fetch();
    }

    //新增action
    public function store(Request $request,TagModel $model){
        $tagName = $request->param('tag_name');
        $positiveScore1 = intval($request->param('positive_score1'));
        $positiveScore2 = intval($request->param('positive_score2'));
        $negativeScore1 = intval($request->param('negative_score1'));
        $negativeScore2 = intval($request->param('negative_score2'));
        $data = [
            'tag_name'=>$tagName,
            'positive_score1'=>$positiveScore1,
            'positive_score2'=>$positiveScore2,
            'negative_score1'=>$negativeScore1,
            'negative_score2'=>$negativeScore2,
        ];
        $res = $model->save($data);
        if (!$res) {
            return Util::apiResponse([], '操作失败', -1);
        }
        return Util::apiResponse([], '', 1);
    }

    public function edit($id,TagModel $model){
        $tagInfo =  $model->where(['id'=>$id])->find();
        $this->assign('tag_info',$tagInfo);
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function update($id,TagModel $model,Request $request){
        $positiveScore1 = intval($request->param('positive_score1'));
        $positiveScore2 = intval($request->param('positive_score2'));
        $negativeScore1 = intval($request->param('negative_score1'));
        $negativeScore2 = intval($request->param('negative_score2'));
        $data = [
            'positive_score1'=>$positiveScore1,
            'positive_score2'=>$positiveScore2,
            'negative_score1'=>$negativeScore1,
            'negative_score2'=>$negativeScore2,
        ];
        $res = $model->save($data,['id'=>$id]);
        if (!$res) {
            return Util::apiResponse([], '操作失败', -1);
        }
        return Util::apiResponse([], '', 1);
    }

    public function delete($id, TagModel $model)
    {
        $exist = $model->find($id);
        if (!$exist) {
            return Util::apiResponse([], '该记录不存在', -1);
        }
        $res = $model->destroy($id);
        if (!$res) {
            return Util::apiResponse([], '删除失败', -1);
        }
        return Util::apiResponse([], '', 1);
    }
}
